package org.mclll.xwangplugin.resourcePack.command;

import cn.hutool.core.util.ZipUtil;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.mclll.xwangplugin.MclllCustom;
import org.mclll.xwangplugin.resourcePack.NetWork;
import org.mclll.xwangplugin.resourcePack.config.PackConfig;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author llB
 * @date 2024-06-19 22:46
 * @description todo
 */
public class PackUploadCommand  implements TabExecutor {
    private MclllCustom mclllCustom;
    public PackUploadCommand(MclllCustom mclllCustom) {
        this.mclllCustom = mclllCustom;
    }


    @Override
    public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
        if (!sender.isOp()) {
            return false;
        }
        switch (args[0]) {
            case "send":
                if (sender instanceof Player && args.length == 1) {
                    NetWork.setPlayerPack((Player) sender);
                    return true;
                } else if (args.length == 2) {
                    String argN = args[1];
                    if ("all".equals(argN)) {
                        for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
                            NetWork.setPlayerPack(onlinePlayer);
                        }
                    } else {
                        Player player = Bukkit.getPlayer(argN);
                        if (player != null && player.isOnline()) {
                            NetWork.setPlayerPack(player);
                        }
                    }
                    return true;
                }
                break;
            case "up":
                NetWork.upPack(mclllCustom.getDataFolder(), sender);
                return true;
            case "pack2o":
                String packPath = PackConfig.getPackPath();
                File dataFolder = mclllCustom.getDataFolder();
                File pack = new File(dataFolder.getParentFile(), packPath);
                if (!pack.exists()) {
                    sender.sendMessage(Component.text(String.format("【%s】路径下资源包不存在", packPath)).color(NamedTextColor.RED));
                    return true;
                }
                File distSrc = new File(dataFolder.getParentFile(), "Oraxen/pack/assets");
                // distSrc目录清空
                for (File file : distSrc.listFiles()) {
                    file.delete();
                }
                File unZipPath = new File(dataFolder, "tmp");
                // 如果tmp夹存在则尝试清空
                if (unZipPath.exists()) {
                    unZipPath.delete();
                }
                ZipUtil.unzip(pack, dataFolder);
                File assets = new File(dataFolder, "tmp/assets");
                // assets下所有的文件夹copy到dataFolder下
                for (File file : assets.listFiles()) {
                    File dest = new File(distSrc, file.getName());
                    if (!dest.exists()) {
                        file.renameTo(dest);
                    }
                }
                break;
            default:
        }
        return false;
    }



    @Override
    public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
        if (args.length == 1) {
            return sender.isOp() ? Arrays.asList("up", "send", "meg2o") : null;
        } else if (args.length == 2 && "send".equals(args[0])) {
            List<String> ret = new ArrayList<>();
            ret.add("all");
            ret.addAll(
                    Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList())
            );
            return ret;
        }
        return null;
    }
}
